package org.team4.dao;

import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.hibernate.SessionFactory;
import org.team4.domain.Syslog;
import org.team4.exception.CRUDException;
import org.team4.util.LoginBean;

import org.team4.dao.GenericDAO;
import org.team4.dao.SyslogDAO;

/**
 * 系统日志DAO
 * 
 * @author MarkerKing
 *
 */
public class SyslogDAO extends GenericDAO<Syslog> {

	private static final long serialVersionUID = -9137703804073963031L;
	private static SyslogDAO instance;
	
	public static SyslogDAO getInstance(SessionFactory sessionFactory) {
        if(instance == null) {
            instance =  new SyslogDAO(sessionFactory);
        }
        return instance;
    }
	
	private SyslogDAO(SessionFactory sessionFactory) {
		super(sessionFactory, Syslog.class);
	}

	/**
	 * 保存系统日志
	 * 
	 * @param contents
	 * @param type
	 * @throws CRUDException
	 */
	public void saveSyslog(String contents, String type) throws CRUDException {
		Syslog syslog = new Syslog();
		syslog.setContents(contents);
		syslog.setType(type);
		syslog.setOpTime(new Date());
		syslog.setOptor(LoginBean.getLoginBean().getUser());
		syslog.setIpAdd(ServletActionContext.getRequest().getRemoteAddr());
		save(syslog);
	}

}